﻿function loadVisitViewModel() {
    //ViewModel
    viewModel =
    {
        IsVisitTextVisible: ko.observable(true)
        , IsPicturesVisible: ko.observable(false)
        , IsFeedsVisible: ko.observable(false)
        , IsClosedVisitsVisible: ko.observable(false)
        , IsClosedVisitsLoaded: ko.observable(false)
        , ClosedVisits: ko.observableArray([])
        , Pictures: ko.observableArray([])

        , ShowTab: function (tabId) {
            viewModel.IsVisitTextVisible(false);
            viewModel.IsFeedsVisible(false);
            viewModel.IsPicturesVisible(false);
            viewModel.IsClosedVisitsVisible(false);

            if (tabId == "VisitText") {
                viewModel.IsVisitTextVisible(true);
            }
            else if (tabId == "Feeds") {
                viewModel.IsFeedsVisible(true);
            }
            else if (tabId == "ClosedVisits") {
                viewModel.IsClosedVisitsVisible(true);
            }
            else if (tabId == "Pictures") {
                viewModel.IsPicturesVisible(true);
            }
        }

       , GetClosedVisits: function (visitId) {
           var service = new mal.balades.Web.Services.VisitServices();
           service.GetClosedVisits(visitId, viewModel.onGetClosedVisitsSuccess, null, null);
       }

        , onGetClosedVisitsSuccess: function (result) {
            viewModel.ClosedVisits(result);
            viewModel.IsClosedVisitsLoaded(true);
        }

        , addPicture: function (pictureId, url, title) {

            var picture = new PictureViewModel();
            picture.load(pictureId, url, title);

            picture.Number(viewModel.Pictures().length + 1);
            viewModel.Pictures.push(picture);

            if (viewModel.Pictures().length == 1)
                picture.show();
            else
                picture.hide();
        }

        , selectPicture: function (picture) {

            this.ShowTab('Pictures');

            if ($(window).scrollTop() > $('#visitTabs').offset().top)
                $(window).scrollTop(-1000);
            for (var i = 0; i < viewModel.Pictures().length; i++) {
                if (viewModel.Pictures()[i] == picture)
                    viewModel.Pictures()[i].show();
                else
                    viewModel.Pictures()[i].hide();
            }
        }
    };
}